//
//  AppDelegate.m
//  ICrEAM
//
//  Created by mac on 16/10/14.
//  Copyright © 2016年 mac. All rights reserved.
//           ______________________
//          /╭-----------—--—-—--—╮\
//          ||                    ||
//          ||    I LOVE iOS!     ||
//          ||                    ||
//          ||  NSLog(@"hello")   ||
//          ||                    ||
//          |╰--------------------╯|\
//          ╰.___________________.╯—\--、_
//                  //___\\           )    、
//        ___________________________/___   \
//       /  oooooooooooooooo  .o.  oooo /, ,—\---╮
//      / ==ooooooooooooooo==.o.  ooo= // /   ▓  /
//     /_==__==========__==_ooo__ooo=_/' /______/
//     "=============================“
//

#import "AppDelegate.h"
#import "NetworkReach.h"
#import "CofigViewController.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
#import "YTKNetworkConfig.h"
#import "YTKUrlArgumentsFilter.h"

#import "LoginViewController.h"
#import "PushAction.h"

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@interface AppDelegate ()
#else

@interface AppDelegate ()
#endif

@end
@implementation AppDelegate
@synthesize netReach;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    /// 需要区分iOS SDK版本和iOS版本。
//if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
//#ifdef NSFoundationVersionNumber_iOS_9_x_Max
//    
//#endif
//    }
   
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else
#endif
    {
        /// 去除warning
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#pragma clang diagnostic pop
    }
    
    if (IsStrEmpty([[ICreamCache sharedInstance]stringForKey:kIPPath])) {
        CofigViewController  *main=[[CofigViewController alloc]init];
        main.hideNav=YES;
        main.hideRightNavItem=YES;
        main.hideSuspendView=YES;
        self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:main];
    }else{
        LoginViewController  *main=[[LoginViewController alloc]init];
        main.hideNav=YES;
        main.hideRightNavItem=YES;
        main.hideSuspendView=YES;
        self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:main];
    }
#if TARGET_IPHONE_SIMULATOR
    Byte dt[32] = {0xc6, 0x1e, 0x5a, 0x13, 0x2d, 0x04, 0x83, 0x82, 0x12, 0x4c, 0x26, 0xcd, 0x0c, 0x16, 0xf6, 0x7c, 0x74, 0x78, 0xb3, 0x5f, 0x6b, 0x37, 0x0a, 0x42, 0x4f, 0xe7, 0x97, 0xdc, 0x9f, 0x3a, 0x54, 0x10};
    [self application:application didRegisterForRemoteNotificationsWithDeviceToken:[NSData dataWithBytes:dt length:32]];
#endif
    //悬浮菜单注入
    
    /*初始化网络判断*/
    [self.netReach initNetwork];
    [self.window makeKeyAndVisible];
//    [self setupNetworkConfig];
  
    return YES;
}



+ (AppDelegate *)currentAppDelegate
{
    
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
}
+(MainViewController*)mainViewController{
    UINavigationController *nav=(UINavigationController*)[AppDelegate currentAppDelegate].window.rootViewController;
    return [nav.viewControllers objectAtIndex:0];
}
- (BOOL)isNetReachable
{
    return self.netReach.isNetReachable;
}
- (NetworkReach *)netReach
{
    if (!netReach) {
        netReach = [[NetworkReach alloc] init];
    }
    return netReach;
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    
    [application registerForRemoteNotifications];
    
    
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    [ICrEAMUser shareICrEAMUser].deviceToken=deviceToken;
 
}
//- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{
//    
//    [ICrEAMUser shareICrEAMUser].deviceToken=pToken;
//    
// 
//}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    
//    NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"];

}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {
    
    NSDictionary *dic=userInfo[@"aps"];
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"消息" message:dic[@"alert"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 || application.applicationState>0) {
        
        
    }
    
    completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
  
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{

}

- (void)applicationWillResignActive:(UIApplication *)application {
       [self clearnBadgeNumber];
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
       [self clearnBadgeNumber];
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
       [self clearnBadgeNumber];
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window NS_AVAILABLE_IOS(6_0){
    return UIInterfaceOrientationMaskLandscape;
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [[ICreamCache sharedInstance] removeDataForKey:HomePagePath];
    [[ICreamCache sharedInstance] removeDataForKey:CustomName];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)clearnBadgeNumber
{

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

@end
